package com.dgty.dg_common.base

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.dgty.dg_common.R
import com.dgty.dg_common.util.LoadingViewUtil
import com.dgty.dg_common.widget.TopbarView
import com.dgty.dg_common.widget.TopbarView.OnButtonClickListener
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar

/**
 * Title: BaseActivity.kt
 * Description:Activity基类
 * Created DateTime: 2023-02-07
 * Created by xxl.
 */
abstract class BaseActivity<T : ViewDataBinding> : AppCompatActivity() {

    val TAG: String = this.javaClass.simpleName
    lateinit var mBind: T
    lateinit var topbarView: TopbarView
    lateinit var onButtonClickListner: OnButtonClickListener
    var isShowTopbar: Boolean = false
//    protected var rootView: View? = null

    abstract fun getLayoutID(): Int

    abstract fun initView()
    abstract fun initData()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: ")
        mBind = DataBindingUtil.setContentView(this, getLayoutID())
        setBaseData()
        initView()
        initData()

    }

    fun immersionBarDark(isDark: Boolean) {
        ImmersionBar.with(this).reset().statusBarDarkFont(isDark).init()
    }

    fun imersionBar() {
        ImmersionBar.with(this).init()
    }


    private fun setGrayTheme() {
        val paint = Paint()
        val cm = ColorMatrix()
        cm.setSaturation(0F)
        paint.colorFilter = ColorMatrixColorFilter(cm)
        window.decorView.setLayerType(View.LAYER_TYPE_HARDWARE, paint)
    }

    private fun setBaseData() {
        onButtonClickListner = object : OnButtonClickListener {
            override fun onLeftClick() {
                finish()
            }

            override fun onRightClick() {

            }

        }
    }

    /**
     * 显示基础的topbar
     */
    fun setShowBar(rootLayout: ViewGroup, resId: Int, rightValue: String = "", listner: OnButtonClickListener = onButtonClickListner) {
        setShowBar(rootLayout, getString(resId), rightValue, listner)
    }


    fun setShowBar(rootLayout: ViewGroup, title: String, rightValue: String = "", listner: OnButtonClickListener = onButtonClickListner) {
        this.isShowTopbar = true
        topbarView = TopbarView(this)
        topbarView.setTitle(title)
        topbarView.setRightValue(rightValue)
        topbarView.setOnButtonClickListener(listner)
        rootLayout.addView(topbarView, 0)
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart: ")
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        Log.d(TAG, "onRestoreInstanceState: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop: ")
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        Log.d(TAG, "onSaveInstanceState: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy: ")
        mBind.unbind()
    }

    fun showLoadingDialog() {
        Log.d(TAG, TAG + "showLoadingDialog: ")
        LoadingViewUtil.showLoadingDialog(this, true)
    }

    fun dismissLoadingDialog() {
        Log.d(TAG, TAG + "dismissLoadingDialog: ")
        LoadingViewUtil.dismissLoadingDialog()
    }

    //状态栏颜色
    private val barColor: Int = R.color.white
    private val statusBarColor: Int = R.color.color_33000000
    var showBarAndPadding = 0
    var hideBarAndPadding = 1
    private val darkText = true

    @SuppressLint("ResourceType")
    protected open fun immersionBarShow(type: Int) {
        var immersionBar = ImmersionBar.with(this)
        if (type == showBarAndPadding) {
            immersionBar.reset()
            immersionBar.hideBar(BarHide.FLAG_SHOW_BAR)
            immersionBar.statusBarColor(barColor) //状态栏颜色
                .navigationBarColor(statusBarColor) //导航栏颜色
                .statusBarDarkFont(darkText) //状态栏深色字体
                .statusBarAlpha(0.0f) //状态栏透明度
                .navigationBarAlpha(0.4f) //导航栏透明度
                .fullScreen(true) //全屏
            immersionBar.init()

        } else if (type == hideBarAndPadding) {
//            immersionBar.reset()
//            immersionBar.hideBar(BarHide.FLAG_HIDE_BAR)
//                .statusBarAlpha(0.0f)
//                .statusBarColor(R.color.white)
//                .statusBarDarkFont(false)
//                .autoDarkModeEnable(true)
//                .autoStatusBarDarkModeEnable(true,0.2f)
//                .fullScreen(true) //全屏
//            immersionBar.init()
            ImmersionBar.with(this)
                .statusBarDarkFont(true) //原理：如果当前设备支持状态栏字体变色，会设置状态栏字体为黑色，如果当前设备不支持状态栏字体变色，会使当前状态栏加上透明度，否则不执行透明度
                .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
                .init()
        }
    }

    override fun attachBaseContext(newBase: Context?) {
        Log.e(TAG, "attachBaseContext")
        super.attachBaseContext(newBase)
//        super.attachBaseContext(MultiLanguageUtil.attachBaseContext(newBase))
//        //app杀进程启动后会调用Activity attachBaseContext
//        MultiLanguageUtil.getInstance().setConfiguration(newBase)

    }
    /* */
    /**
     * 系统ui状态变化通知
     *//*
    fun initSystemUIListener() {
        val decorView = window.decorView
        decorView.setOnSystemUiVisibilityChangeListener { visibility ->
            if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) { //The system bars are visible
                // 控制状态栏
                StatusUtil.setBaseStatusBar(this)
            }
        }
    }*/
}